# -*- coding:utf-8 -*-
"""
该类提供操作页面的所有基础方法。
"""
from bs4 import BeautifulSoup
import urllib.request
import random
import re
from utils.Regex import Regex

class Spider():
    def __init__(self, url):
        self.url = url

    def __getUserAgent(self):
        """
        随机获取一个UserAgent
        :return: UserAgent
        """
        with open("../config/user-Agent") as f:
            date = f.read()

        userAgnetList = date.split("\n")

        return userAgnetList[random.randrange(0,len(userAgnetList))]

    def __getHeaders(self):
        """
        从headers文件中读取headers
        :return: headers
        """
        with open("../config/headers") as f:
            date = f.read()

        headers = {}

        headers["user-Agent"] = self.__getUserAgent().strip()

        if date != "":
            for header in date.split("\n"):
                key, value = header.split(":", 1)
                headers[key.strip()] = value.strip()

        return headers

    def _getHtml(self):
        headers = self.__getHeaders()
        request = urllib.request.Request(url=self.url, headers=headers, method="GET")
        response = urllib.request.urlopen(request)
        html = response.read().decode("utf-8")
        return html

    def _parseHtml(self, html):
        with open("./test.html", 'rb') as f:
            date = f.read()

        bs = BeautifulSoup(date, "html.parser")

        divs = bs.find_all("div", id="dismissible")

        info = {}
        for div in divs:
            div = str(div)
            # print(div)
            title = re.findall(Regex.TITLE, div)[0]
            length = re.findall(Regex.LENGTH, div)[0]
            playCount = re.findall(Regex.PLAY_COUNT,div)[0]
            time = re.findall(Regex.PLAY_COUNT,div)[1]

            print(title)
            print(length)
            print(playCount)
            print(time)

            # info[title] = {
            #     # "链接": link,
            #     "内容": comment,
            #     "评分": rating,
            #     "评分人数": estimate
            # }

        return info

    def __writeToExcel(self):
        pass

    def main(self):
        # html = self._getHtml()
        info = self._parseHtml('')
        return info


if __name__ == '__main__':
    spider = Spider("https://movie.douban.com/chart")
    info = spider.main()
    print(info)